package com.cfwx.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ReflectField {

	public int i = 2;
	public boolean flag = true;
	protected String j = new String("hello");
	private Object[] object = {};


	public static void main(String[] args) {
		ReflectField test = new ReflectField();
		Field [] fields = ReflectField.class.getFields();//字节码上对于的对象
		for(int i=0;i<fields.length;i++){
			Field fi = fields[i];
			try{
				String name = fi.getName();//获得此 Field 对象表示的字段的名称
				Object value = fi.get(test);//返回指定对象上此 Field 表示的字段的值
				int moId = fi.getModifiers();
				boolean isPublic = Modifier.isPublic(moId);//判断修饰符是否为public
				Class type = fi.getType();
				if(name.trim().equals("i")){
				fi.set(test, 3);
				System.out.println(value);
				}
				System.out.println(i+"是否为public:"+isPublic);
				System.out.println(name + " = " + value.toString());
				System.out.println(type);
			}
			catch(Exception e){
				fi.setAccessible(true);
				e.printStackTrace();
			}
		}

	}

}
